package com.spring.boot.demo.controller;

import com.alibaba.druid.pool.DruidDataSource;
import com.spring.boot.demo.config.DemoConfig;
import com.spring.boot.demo.mapper.UserMapper;
import com.spring.boot.demo.pojo.User;
import com.zyb.base.pojo.PageVo;
import com.zyb.base.util.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.sql.DataSource;
import java.util.List;

/**
 * @author ZhuYouBin
 * @version 1.0.0
 * @Date: 2022/11/13 20:37
 * @Description
 */
@RestController
@RequestMapping("/api/test")
public class DemoController {
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private DataSource dataSource;

    @Autowired
    private DemoConfig demoConfig;

    @GetMapping("/demo")
    public String demo() {
        System.out.println(dataSource.getClass());
        DruidDataSource source = (DruidDataSource) dataSource;
        System.out.println(source.getUrl());
        System.out.println(source.getActiveCount());
        System.out.println(source.getMinIdle());
        System.out.println("====================");
        System.out.println(demoConfig.getDemoName());
        System.out.println(demoConfig.getDemoPass());
        return "success.";
    }

    @GetMapping("/exp")
    public String exp() {
        int i = 10 / 0;
        return "success.";
    }

    @GetMapping("/pageHelper02")
    public PageVo query02(Integer pageSize, Integer pageNum) {
        PageVo pageVo = new PageVo(pageNum, pageSize);
        // 开启分页
        PageUtil.startPage(pageVo);
        // 查询数据库
        List<User> list = userMapper.queryList();
        // 封装查询结果
        return PageUtil.endPage(pageVo, list);
    }

    @GetMapping("/query")
    public PageVo query03(Integer pageSize, Integer pageNum) {
        PageVo pageVo = new PageVo(pageNum, pageSize);
        // 开启分页
        PageUtil.startPage(pageVo);
        // 查询数据库
        List<User> list = userMapper.queryList();
        // 封装查询结果
        return PageUtil.endPage(pageVo, list);
    }

}
